from tools.utils import Mouse
from tools.virtual_key_code import get_key_state
from bdtime import Time
from tools.mouse_state_recorder import MouseEvents
from tools.mouse_state_recorder import DefaultArgs


class KeyMoveMouse:
    click_left = '0'
    # click_middle = '5'
    # click_right = '4'

    move_left = 'left'
    move_right = 'right'
    move_up = 'up'
    move_down = 'down'


key_move_mouse = KeyMoveMouse()
mouse_events = MouseEvents()

# ms = Mouse(use_rel_move=True)
ms = Mouse(use_rel_move=False)


def run(tt=None):
    tt = tt if tt else Time()

    dc_ls = []
    i = 0
    while True:
        if DefaultArgs.flag__exit:
            break

        # tt.stop_alt('x')
        if tt.stop('alt + x', raise_error=False):
            break

        # tt.sleep(1 / frames)
        next_record_time = DefaultArgs.record_time_schedule[i]
        while tt.now() < next_record_time:
            if DefaultArgs.use_sleep_wait:
                tt.sleep(0.001)
        i += 1

        dc_i = {'time': tt.now()}

        key__click_left = DefaultArgs.key__click_mouse_left
        state__click_left = get_key_state(key__click_left)

        key__shift_speed = DefaultArgs.key__shift_mouse_speed
        state__shift_speed = get_key_state(key__shift_speed)

        state__mouse_speed_slow = get_key_state(DefaultArgs.key__mouse_speed_dc['slow'])
        state__mouse_speed_normal = get_key_state(DefaultArgs.key__mouse_speed_dc['normal'])
        state__mouse_speed_quick = get_key_state(DefaultArgs.key__mouse_speed_dc['quick'])

        state__move_left = get_key_state('left')
        state__move_right = get_key_state('right')
        state__move_up = get_key_state('up')
        state__move_down = get_key_state('down')

        if state__move_left:
            mouse_events.move_left()
        if state__move_right:
            mouse_events.move_right()
        if state__move_up:
            mouse_events.move_up()
        if state__move_down:
            mouse_events.move_down()

        if state__click_left:
            mouse_events.press_left()
        else:
            mouse_events.release_left()

        if state__shift_speed:
            mouse_events.shift_speed()

        if state__mouse_speed_slow:
            mouse_events.change_mouse_speed_to_slow()
        if state__mouse_speed_normal:
            mouse_events.change_mouse_speed_to_normal()
        if state__mouse_speed_quick:
            mouse_events.change_mouse_speed_to_quick()

        dc_i.update({'state__click_left': state__click_left})
        dc_i.update({'state__move_left': state__move_left})
        dc_i.update({'state__move_right': state__move_right})
        dc_i.update({'state__move_up': state__move_up})
        dc_i.update({'state__move_down': state__move_down})

        dc_ls.append(dc_i)

    # print('dc_ls ---', dc_ls)


def main():
    from tools.mouse_state_recorder import run as run__mouse_recorder
    from tools.key_state_recorder import run as run__key_recorder
    from tools.beep import beep
    import threading
    import keyboard
    from bdtime import tt

    hotkey = 'alt + 1'
    print(f'--- wait hotkey[{hotkey}]!')

    keyboard.wait(hotkey)

    tt.sleep(0.5)
    print('--- start main__mouse_recorder!')
    beep()

    mouse_kwargs = {
        'save_file_name': 'run__mouse_recorder',
        'play': True,
        'remove_else_recoded': True,
        'tt': tt
    }
    task_mouse = threading.Thread(target=run__mouse_recorder, kwargs=mouse_kwargs)

    key_kwargs = {
        'save_file_name': 'run__key_recorder',
        'play': True,
        'remove_else_recoded': True,
        'tt': tt
    }
    task_key = threading.Thread(target=run__key_recorder, kwargs=key_kwargs)

    task_mouse.start()
    task_key.start()

    run()


if __name__ == '__main__':
    main()
